package com.richapp.launcher.ui.adapter;

import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.richapp.launcher.R;
import com.richapp.launcher.database.system.mode.DeviceModel;

public class ChildRoomLightGridAdapter extends BaseAdapter {
    private LayoutInflater layoutInflater = null;
    private List<DeviceModel> deviceList;
    Resources res = null;
	public ChildRoomLightGridAdapter(Context context,List<DeviceModel> deviceList){
		this.deviceList = deviceList;
		layoutInflater = LayoutInflater.from(context);
		res = context.getResources();
	}
	
	public void setDeviceList(List<DeviceModel> deviceList){
		this.deviceList = deviceList;
	}
	
	@Override
	public int getCount() {
		if (deviceList == null) {
			return 0;
		}
		return deviceList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return deviceList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	boolean flag = true;
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = layoutInflater.inflate(R.layout.child_gridroomlight_item,null);
			viewHolder.child_gridroomlight_name = (TextView)convertView.findViewById(R.id.child_roomlightgrid_name);
			viewHolder.lamp_switch = (ImageView)convertView.findViewById(R.id.lamp_switch);
		    convertView.setTag(viewHolder);
		}else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
		DeviceModel deviceModel = deviceList.get(position);
		if (deviceModel.getDeviceNumber().equals("2")) {
			viewHolder.child_gridroomlight_name.setText(res.getString(R.string.deviceid_2));
		}else if (deviceModel.getDeviceNumber().equals("3")) {
			viewHolder.child_gridroomlight_name.setText(res.getString(R.string.deviceid_3));
		}
		if (deviceModel.getDeviceStatus() == null || deviceModel.getDeviceStatus().equals("00")) {
			viewHolder.lamp_switch.setImageResource(R.drawable.child_light_off);
		}else {
			viewHolder.lamp_switch.setImageResource(R.drawable.child_light_on);
		}
		return convertView;
	}

	class ViewHolder{
		TextView child_gridroomlight_name = null;
		ImageView lamp_switch = null;
	}
}
